import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import User from '../components/user/User.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'
Vue.use(VueRouter)

const routes = [
    { path: '/', redirect: "/login" },
    { path: '/login', component: Login },
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [
            { path: '/welcome', component: Welcome },
            { path: '/users', component: User },
            { path: '/rights', component: Rights },
            { path: '/roles', component: Roles },
            { path: '/categories', component: Cate }
        ]
    }
]

const router = new VueRouter({
        routes
    })
    //设置导航守卫 没有登录不允许访问页面
router.beforeEach((to, from, next) => {
    //to表示跳转的路径对象 from从哪里跳转过来的路径对象 next表示放行 有参数则表示跳转到指定路径
    if (to.path == '/login') return next()
        //如果不是登录页面 则需要获取token 有则放行 没有则跳转到登录页面
    const token = sessionStorage.getItem('token');
    if (!token) return next('/login')
    next()
})
export default router